var dragged;
var origine;
var skillsAffiches = false
var filtreActif = false;

window.addEventListener('load', initTeam, false);
  /* events fired on the draggable target */
  document.addEventListener("drag", function( event ) {

  }, false);

  document.addEventListener("dragstart", function( event ) {
      // store a ref. on the dragged elem
      dragged = event.target;
      origine = dragged.parentElement;
      // make it half transparent
      event.target.style.opacity = .5;
  }, false);

  document.addEventListener("dragend", function( event ) {
      // reset the transparency
      event.target.style.opacity = "";
  }, false);

  /* events fired on the drop targets */
  document.addEventListener("dragover", function( event ) {
      // prevent default to allow drop
      event.preventDefault();
  }, false);

  document.addEventListener("dragenter", function( event ) {
      // highlight potential drop target when the draggable element enters it

  }, false);

  document.addEventListener("dragleave", function( event ) {
      // reset background of potential drop target when the draggable element leaves it
      if ( event.target.id == "Team" ) {
          event.target.style.background = "";
      }

  }, false);

  document.addEventListener("drop", function( event ) {
      // prevent default action (open as link for some elements)
      dropTank(event);
      // move dragged elem to the selected drop target
      
    
  }, false);
  
  document.addEventListener('dblclick', function (event) {
	  clickTank(event.target);
  }, false)
  
   function initTeam() {
	 var frm = document.getElementById('FrmEquipe');
	 if (frm != null) {
		 frm.addEventListener("submit", function (event) {
			 setFrmTeam();
		 },false);
		 
	 }
	 compterPoints();
	 var Team = document.getElementById('Team');
	 var filtre = document.getElementById('filtres');
	 if (filtre != null) {
		 lstFiltres = document.getElementById('divChoixFiltre').children[0].children;
		 for (f = 0; f < lstFiltres.length; f++) {
			 lstFiltres[f].addEventListener('click', function(event) {activerFiltre(event)}, false)
		 }
	 }
	 Team.style.height = Team.childNodes[3].style.height;
 }

  function clickTank(target) {
	  if(target.classList[0] == "minitank") {
		  var account_id = getAccountId(target.id);
		  if(target.parentElement.parentElement.parentElement.id == ('Team')) {
			  removeTankTeam(target);
		  }
		  else if (target.parentElement.id == account_id) {
			  addTankTeam(target);
		  }
		  //source.removeChild(target);
		  document.getElementById('Team').style.height = document.getElementById('Team').childNodes[3].style.height;
		  compterPoints();
	  }
	  return false;
  }
   
 function addTankTeam(Tank) {
	 var Team = document.getElementById('Team');
	 var DockMembre = document.getElementById(getAccountId(Tank.id));
	 var present = dejaPresent(getPseudo(Tank.id));
	 if (present == false) {
		 addTank(Team, Tank);
     }
	 else {		 
		 addTank(Team, Tank);
		 present.parentElement.parentElement.removeChild(present.parentElement);
		 addTank(DockMembre, present);
	 }
 }
 
 function addTank(dock, Tank) {
	 if (dock.id == 'Team') {
		 var membre = document.createElement('li');
	     membre.appendChild(Tank);
	     var pseudo = document.createTextNode(getPseudo(Tank.id));
	     membre.appendChild(pseudo);
	     dock.childNodes[3].appendChild(membre);
	 }
	 else {
		 dock.appendChild(Tank);
	 }
 }
 
 function removeTankTeam(Tank) {
	 var source = document.getElementById('Team').childNodes[3];
	 var destination = document.getElementById(getAccountId(Tank.id));
	 source.removeChild(Tank.parentElement);
	 addTank(destination, Tank);
 }
 
 function removeTank(dock, Tank) {
	 if (dock.parentElement.parentElement.id == 'Team') {
		 dock.parentElement.removeChild(dock);
	 }
	 else {
		 dock.removeChild(Tank);
	 }
 }
 
 function addTankMembre(Tank) {
	 var dockMembre = document.getElementById(getAccountId(Tank.id));
	 var parent = Tank.parentElement;
	 dockMembre.appendChild(Tank);
 }
 
 function setFrmTeam() {
	 var TeamMembers = document.getElementById('Team').childNodes[3].childNodes;
	 var chaine = '';
	 for (i = 0; i < TeamMembers.length; i++) {
		 chaine += TeamMembers[i].childNodes[0].id+'#';
	 }
	 document.getElementById('TeamMembers').value = chaine.substr(0, chaine.length-1);

 }
 
  function dropTank(event) {
	  event.preventDefault();
	  var dock = getTeamDock(event.target)
	  var limite = getLimite();
	  if ( dragged.classList[0] == "minitank") {
		  var present = dejaPresent(getPseudo(dragged.id));
		  if (dock != false) {
			  document.getElementById('Team').style.background = "";
	          dragged.parentNode.parentNode.removeChild( dragged.parentNode );
	          if (present != false) {
	        	  var dockMembre = document.getElementById(getAccountId(dragged.id));
	        	  var parent = present.parentElement;
	        	  dockMembre.appendChild(present);
	        	  dock.removeChild(parent);
	          }
	          var membre = document.createElement('li');
	          membre.appendChild(dragged.parentNode);
	          var pseudo = document.createTextNode(getPseudo(dragged.id));
	          membre.appendChild(pseudo);
	          dock.appendChild(membre);
	          compterPoints();
	          dock.parentElement.style.height = dock.style.height;
		  }
		  else {
			  if (present != false) {
	        	  var dockMembre = document.getElementById(getAccountId(dragged.id));
	        	  var parent = present.parentElement;
	        	  var Team = document.getElementById('Team');
	        	  dockMembre.appendChild(present);
	        	  Team.childNodes[3].removeChild(parent);
	        	  compterPoints();
	          }
		  }
      }
  }
  
  function getTeamDock(target) {
	  if (origine.parentElement.parentElement.className != 'inscrits') {
		  return false;
	  }
	  if (target.id == 'Team') {
		  return target.childNodes[3];
	  }
	  switch (target.tagName) {
		  case 'LI':
			  if (target.parentElement.parentElement.id == 'Team') {
				  return target.parentElement;
			  }
		  break;
		  
		  case 'IMG':
			  if (target.parentElement.parentElement.parentElement.parentElement.id == 'Team') {
				  return target.parentElement.parentElement.parentElement;
			  }
		  break;
		  
		  case 'DIV':
			  if (target.parentElement.id == 'Team'){
				  return target.parentElement.childNodes[3];
			  }
			  if (target.className == 'scoreTeam') {
				  return target.parentElement.parentElement.parentElement;
			  }
	  }
	  return false;
  }
  
  function getPseudo(chaine) {
	  var data = chaine.split('|');
	  return data[2];
  }
  
  function getAccountId(chaine) {
	  var data = chaine.split('|');
	  return data[0];
  }
  
  function getTankId(chaine) {
	  var data = chaine.split('|');
	  return data[1];
  }
  
  function getLevel(chaine) {
	  var data = chaine.split('|');
	  return Number(data[3]);
  }
  
  function dejaPresent(pseudo) {
	  var Team = document.getElementById('Team');
	  for (i = 0; i < Team.childNodes[3].childNodes.length; i++) {
		  var ligne = Team.childNodes[3].childNodes[i];
		  
		  if (ligne.childNodes[0].childNodes[0].tagName = 'IMG' && ligne.childNodes[0].childNodes[0].classList[0] == 'minitank') {
			  var pseudoAccepte = getPseudo(ligne.childNodes[0].childNodes[0].id);
			  if(pseudoAccepte == pseudo) {
				  return ligne.childNodes[0];
			  }
		  }
	  }
	  return false;
  }
  
  function getLimite() {
	  var limit = document.getElementById('limite');
	  return Number(limit.innerText);
  }
  
  function compterPoints() {
	  var decompte = Number(0);
	  var Team = document.getElementById('Team');
	  var points = document.getElementById('points');
	  for (i = 0; i < Team.childNodes[3].childNodes.length; i++) {
		  var ligne = Team.childNodes[3].childNodes[i];
		  
		  if (ligne.childNodes[0].childNodes[0].tagName = 'IMG' && ligne.childNodes[0].childNodes[0].classList[0] == 'minitank') {
			  var level = getLevel(ligne.childNodes[0].childNodes[0].id);
			  decompte+=level;
			  
		  }
	  }
	  points.innerText = decompte;
  }
  
  function activerFiltre(event) {
	  id = event.target.id.substr(7);
	  
	  var lstImg = document.getElementsByTagName('img') 
	  for (img = 0; img < lstImg.length; img++) {
		  if(lstImg[img].classList[0] == 'minitank') {
			  if ( id == 'false' || in_array(getTankId(lstImg[img].id), filtres[id].tanks) == true) {
				  lstImg[img].parentElement.style.display = 'inline-block';
			  }
			  else {
				  lstImg[img].parentElement.style.display = 'none';
			  }
		  }
	  }
  }
  